' This script will demonstrate how one can use the HistoryState object to
' "undo" commands.  In this example, the HistoryState is stored, and then
' 2 methods are called simply to modify the HistoryState.  When the ActiveHistoryState
' is reset to the previously saved HistoryState, the actions are "rolled back" to that
' state.  This essentially does an "undo" of the 2 methods called.

Private Sub Command1_Click()

Dim appRef As Photoshop.Application
Dim docRef As Photoshop.Document
Dim currentHistory As Photoshop.HistoryState

Set appRef = New Photoshop.Application

If (appRef.Documents.Count > 0) Then

    Set docRef = appRef.ActiveDocument
    Set currentHistory = docRef.HistoryStates(1)
    
    ' Do a couple of things to change the history state.  This adds
    ' 2 items to the HistoryStates collection.
    docRef.ActiveLayer.AdjustBrightnessContrast 30, 40
    docRef.ActiveLayer.AdjustLevels 20, 100, 2, 30, 120
    
    ' This rolls back the history to the beginning.
    ' The "beginning" refers to HistoryStates(1)
    docRef.ActiveHistoryState = currentHistory
    
Else
    MsgBox ("You must have at least one document open to run this script")
End If

End Sub
